#include "manager.h"
#include "NetCap.h"
//#include <pthread.h>

#ifdef WIN32
#include <process.h>
#else
#endif

int cmp(const pair<uint64_t,macinfo>& vect1, const pair<uint64_t,macinfo>&vect2)    
{    
     return ( (vect1.second).times > (vect2.second).times) ;
} 


manager:: manager()
{
    
}
manager:: ~manager()
{
    for( map<unsigned int, NetCap*> ::iterator it; it != caplist.end();++it)
    {
        delete it->second;
        it->second = NULL;
    }
}
int manager::ping(const char* ip, int bfs, int timeout, int& usedtime)
{
    return 0;
}

int manager::ipDetect(string ip)
{
    
    return 0;
}
 

string  manager::getMacs(unsigned int seq,std::vector<pair<uint64_t,macinfo> >  &vmac)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
    if( it !=  caplist.end())
    {
        (it->second)-> getmacs(vmac);
        sort(vmac.begin(), vmac.end(), cmp);
        //for(int i=0;i<vmac.size();i++)   
        //{    
        //    cout<<"mac:"<<vmac[i].first<<"size: "<<vmac[i].second<<endl; 
      // }
    }
    return " ";
}

int manager::getlinkstat(unsigned int seq, std::vector<linkstat>&linkstats)
{
	map<unsigned int, NetCap*> ::iterator it;
	it= caplist.find(seq);
	if( it !=  caplist.end())
	{
		(it->second)->getlinkstat(linkstats);
		return 0;
	}
	else
	{
		 return -1;
	}
}


#if  1
int manager::getdetect(unsigned int seq ,std::map<std::string ,uint64_t>&ips)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
    if( it !=  caplist.end())
    {
        //printf("getdetect\n");
        (it->second)-> getdetect(ips);
        return 0;
    }
    else{
        return -1;
    }
}
#endif
int manager::getFlows(unsigned int seq)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
    if( it !=  caplist.end())
    {
        return (it->second)->getNetCradFlow();
    }
    else
    {
        return 0;
    }
    
}
int manager::getUpFlows(unsigned int seq)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
     if( it !=  caplist.end())
     {
        return (it->second)->getLocalNetUpFlow();
     }
     else
     {
        return 0;
     }
    
}
unsigned int manager::getnetflag(unsigned int seq)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
    if( it !=  caplist.end())
    {
        return (it->second)->getnetflag();
    }
    else
    {
        return 0;
    }
    
    
}

int manager::getDownFlows(unsigned int seq)
{
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq);
    if( it !=  caplist.end())
    {
        return (it->second)->getLocalNetDownFlow();
    }
    else
    {
        return 0;
    }
    
    
}
 void * manager::capThreadCreate(void* param)
{
    if(param != NULL)
    {
        NetCap* pcap = (NetCap*)param;
        pcap->CreateCap(1,pcap->getFilterRule());
    }
	return NULL;
}

bool manager::startCap(unsigned int seq)
{
    #if 1
        map<unsigned int, NetCap*> ::iterator it;
        it= caplist.find(seq); 
        if ( it != caplist.end())
        {
        #ifdef WIN32
            int threadid; 
            threadid  = _beginthreadex(NULL, 0, capThreadCreate,it->second, 0, NULL);
            (it->second)->threadid = threadid;
	    #else		
          pthread_t  threadid;
          if( pthread_create(&threadid, NULL, capThreadCreate,it->second) != 0) 
            {		
                printf("pthread_create failed! \n");
                return 1 ;
            }
           (it->second)->threadid=threadid;
         #endif   
            return 0;
            
        }
        else
        {
            return 1 ;
        }
        #endif
}
void manager::createCap(string rule,int seq)
{
    NetCap * pcap;
    map<unsigned int, NetCap*> ::iterator it;
    it= caplist.find(seq); 
    if ( it == caplist.end())
    {
        pcap = new NetCap(rule);
        caplist.insert(pair<unsigned int, NetCap*>(seq,pcap));
    }
    
}
void manager::startipdetect(int seq)
{
    printf("startip cap ***************************************************:%d\n",seq );
    map<unsigned int, NetCap*> ::iterator it;
    it = caplist.find(seq); 
    if ( it != caplist.end())
    {
         (it->second)->ipdetectcontrl = 1 ;
    }
    printf("ending  startip cap ***************************************************:%d\n",seq );
}
void manager::stopipdetect(int seq)
{
    printf("stop cap ***************************************************:%d\n",seq );
    map<unsigned int, NetCap*> ::iterator it;
    it = caplist.find(seq); 
    //sleep(10);
    if ( it != caplist.end())
    {
         (it->second)->ipdetectcontrl = 0 ;
    }
}

void manager::stopCap(int seq)
{
    printf("stop cap :%d\n",seq );
    map<unsigned int, NetCap*> ::iterator it;
    it = caplist.find(seq); 
    //sleep(10);
    if ( it != caplist.end())
    {
        #ifdef WIN32
            
	  #else
          pthread_cancel((it->second)->threadid1); 
          int i =  pthread_cancel((it->second)->threadid) ;
          if( i ==0)
          {
              printf("stop sucessful\n");
          }
          else
          {
              printf("stop failure\n");
          }
        #endif
        delete it->second;
        caplist.erase(it);
    }
}
